//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "MultiStepSwaption.h"
using namespace Cephei::QL::Models::Marketmodels::Products::Multistep;
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModelMultiProductCashFlow.h>
#include <gen/QL/Instruments/StrikedTypePayoff.h>
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/Products/MultiProductMultiStep.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL::Models::Marketmodels::Products;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::CMultiStepSwaption (Cephei::IVector<Double>^ rateTimes, UInt64 startIndex, UInt64 endIndex, Cephei::QL::Instruments::IStrikedTypePayoff^ payOff) : CMultiProductMultiStep(CMultiStepSwaption::typeid)
{
    CoVector<Double>^ _CrateTimes;
    CStrikedTypePayoff^ _CpayOff;
    try
    {
#ifdef HANDLE
        _phMultiStepSwaption = NULL;
#endif
        CoVector<Double>^ _CrateTimes = safe_cast<CoVector<Double>^> (rateTimes);
        _CrateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = _CrateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        QuantLib::Size _startIndex = (QuantLib::Size)ValueHelper::Convert (startIndex);
        QuantLib::Size _endIndex = (QuantLib::Size)ValueHelper::Convert (endIndex);
        _CpayOff = safe_cast<CStrikedTypePayoff^> (payOff);
        _CpayOff->Lock();
        boost::shared_ptr<QuantLib::StrikedTypePayoff>& _payOff = static_cast<boost::shared_ptr<QuantLib::StrikedTypePayoff>&> (_CpayOff->GetShared ()); 
        _ppMultiStepSwaption = new boost::shared_ptr<QuantLib::MultiStepSwaption> (new QuantLib::MultiStepSwaption ( _rateTimes,  _startIndex,  _endIndex,  _payOff ));
        SetMultiProductMultiStep (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepSwaption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CrateTimes != nullptr) _CrateTimes->Unlock();
        if (_CpayOff != nullptr) _CpayOff->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::CMultiStepSwaption (boost::shared_ptr<QuantLib::MultiStepSwaption>& childNative, Object^ owner) : CMultiProductMultiStep(CMultiStepSwaption::typeid)
{
#ifdef HANDLE
	_phMultiStepSwaption = NULL;
#endif
	_ppMultiStepSwaption = &childNative;
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepSwaption));
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::CMultiStepSwaption (QuantLib::MultiStepSwaption& childNative, Object^ owner) : CMultiProductMultiStep(CMultiStepSwaption::typeid)
{
#ifdef HANDLE
	_phMultiStepSwaption = NULL;
#endif
	_ppMultiStepSwaption = new boost::shared_ptr<QuantLib::MultiStepSwaption> (&childNative);
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepSwaption));
    _MultiStepSwaptionOwner = owner;
    _MultiProductMultiStepOwner = owner;
}

Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::CMultiStepSwaption (CMultiStepSwaption^ copy) : CMultiProductMultiStep(CMultiStepSwaption::typeid)
{
#ifdef HANDLE
	_phMultiStepSwaption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMultiStepSwaption = new boost::shared_ptr<QuantLib::MultiStepSwaption> (copy->GetShared());
        _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepSwaption));
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::CMultiStepSwaption (System::Type^ t) : CMultiProductMultiStep(CMultiStepSwaption::typeid)
{
#ifdef HANDLE
	_phMultiStepSwaption = NULL;
#endif
	if (!t->IsSubclassOf(CMultiStepSwaption::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::CMultiStepSwaption (QuantLib::Handle<QuantLib::MultiStepSwaption>& childNative, Object^ owner)  : CMultiProductMultiStep(CMultiStepSwaption::typeid)
{
	_phMultiStepSwaption = &childNative;
	_ppMultiStepSwaption = &static_cast<boost::shared_ptr<QuantLib::MultiStepSwaption>>(childNative.currentLink());
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepSwaption));
    _MultiStepSwaptionOwner = owner;
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::CMultiStepSwaption (QuantLib::Handle<QuantLib::MultiStepSwaption> childNative)  : CMultiProductMultiStep(CMultiStepSwaption::typeid)
{
	_phMultiStepSwaption = &childNative;
	_ppMultiStepSwaption = &static_cast<boost::shared_ptr<QuantLib::MultiStepSwaption>>(childNative.currentLink());
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepSwaption));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::CMultiStepSwaption (QuantLib::MultiStepSwaption childNative)  : CMultiProductMultiStep(CMultiStepSwaption::typeid)
{
#ifdef HANDLE
	_phMultiStepSwaption = NULL;
#endif
	_ppMultiStepSwaption = new boost::shared_ptr<QuantLib::MultiStepSwaption> (new QuantLib::MultiStepSwaption (childNative));
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepSwaption));
}
#endif

Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::~CMultiStepSwaption ()
{
    if (_ppMultiStepSwaption != NULL)
    {
	    delete _ppMultiStepSwaption;
        _ppMultiStepSwaption = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::!CMultiStepSwaption ()
{
    if (_ppMultiStepSwaption != NULL)
    {
	    delete _ppMultiStepSwaption;
    }
}
QuantLib::MultiStepSwaption& Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::GetReference ()
{
    if (_ppMultiStepSwaption == NULL) throw gcnew NativeNullException ();
	return **_ppMultiStepSwaption;
}
boost::shared_ptr<QuantLib::MultiStepSwaption>& Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::GetShared ()
{
    if (_ppMultiStepSwaption == NULL) throw gcnew NativeNullException ();
	return *_ppMultiStepSwaption;
}
QuantLib::MultiStepSwaption* Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::GetPointer ()
{
    if (_ppMultiStepSwaption == NULL) throw gcnew NativeNullException ();
	return &**_ppMultiStepSwaption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MultiStepSwaption>& Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::GetHandle ()
{
	if (_phMultiStepSwaption == NULL)
	{
		_phMultiStepSwaption = new Handle<QuantLib::MultiStepSwaption> (*_ppMultiStepSwaption);
	}
	return *_phMultiStepSwaption;
}
#endif
bool Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::HasNative () 
{
	return (_ppMultiStepSwaption != NULL);
}

Boolean Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::NextTimeStep (Cephei::QL::Models::Marketmodels::ICurveState^ currentState, Cephei::IVector<UInt64>^ numberCashFlowsThisStep, Cephei::IMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ cashFlowsGenerated)
{
    CCurveState^ _CcurrentState;
    CoVector<UInt64>^ _CnumberCashFlowsThisStep;
    CoMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ _CcashFlowsGenerated;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CcurrentState = safe_cast<CCurveState^> (currentState);
        _CcurrentState->Lock();
        QuantLib::CurveState& _currentState = static_cast<QuantLib::CurveState&> (_CcurrentState->GetReference ()); 
        _CnumberCashFlowsThisStep = safe_cast<CoVector<UInt64>^> (numberCashFlowsThisStep);
        _CnumberCashFlowsThisStep->Lock();
        INativeVector<UInt64>^ _NCInumberCashFlowsThisStep = _CnumberCashFlowsThisStep->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCnumberCashFlowsThisStep = safe_cast<CUInt64Vector^>(_NCInumberCashFlowsThisStep);
        std::vector<QuantLib::Size>& _numberCashFlowsThisStep = static_cast<std::vector<QuantLib::Size>&> (_NCnumberCashFlowsThisStep->GetReference ());
        _CcashFlowsGenerated = safe_cast<CoMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^> (cashFlowsGenerated);
        _CcashFlowsGenerated->Lock();
        INativeMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ _NCIcashFlowsGenerated = _CcashFlowsGenerated->getFeature (NativeFeature::Value);
        CMarketModelMultiProductCashFlowMatrix^ _NCcashFlowsGenerated = safe_cast<CMarketModelMultiProductCashFlowMatrix^>(_NCIcashFlowsGenerated);
        std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >& _cashFlowsGenerated = static_cast<std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >&> (_NCcashFlowsGenerated->GetReference ());
    	bool _rv = (bool)(*_ppMultiStepSwaption)->nextTimeStep ( _currentState,  _numberCashFlowsThisStep,  _cashFlowsGenerated );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcurrentState != nullptr) _CcurrentState->Unlock();
        if (_CnumberCashFlowsThisStep != nullptr) _CnumberCashFlowsThisStep->Unlock();
        if (_CcashFlowsGenerated != nullptr) _CcashFlowsGenerated->Unlock();
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::MaxNumberOfCashFlowsPerProductPerStep::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMultiStepSwaption)->maxNumberOfCashFlowsPerProductPerStep ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::NumberOfProducts::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMultiStepSwaption)->numberOfProducts ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::PossibleCashFlowTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppMultiStepSwaption)->possibleCashFlowTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::IMultiStepSwaption^ Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppMultiStepSwaption)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Products::Multistep::IMultiStepSwaption^ Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepSwaption_Factory::Create (Cephei::IVector<Double>^ rateTimes, UInt64 startIndex, UInt64 endIndex, Cephei::QL::Instruments::IStrikedTypePayoff^ payOff)
{
    return gcnew CMultiStepSwaption ( rateTimes,  startIndex,  endIndex,  payOff);
}
